<template>
  <a-layout class="chat-container">
    <!-- 聊天区域 -->
    <a-layout-content class="chat-content">
      <div class="chat-center">
        <div class="room-info">
          <div class="room-id">房间号: {{ roomId }}</div>
          <div class="room-tip">准备好了就说“开始”吧！</div>
        </div>
        <div class="message-list" ref="messageListRef">
          <div v-for="(msg, index) in messages" :key="index"
               :class="['message-item', msg.isAI ? 'ai' : 'user']">
            <a-comment :class="msg.isAI ? 'ai-message' : 'user-message'">
              <template #avatar>
                <a-avatar :src="msg.isAI ? '/images/ai-avatar.png' : '/images/user-avatar.png'"
                          class="message-avatar" />
              </template>
              <template #content>
                <div :class="['message-bubble', msg.isAI ? 'ai' : 'user']">
                  {{ msg.content }}
                </div>
              </template>
            </a-comment>
          </div>
        </div>
      </div>
    </a-layout-content>

    <!-- 输入区域 -->
    <a-layout-footer class="input-area">
      <div class="input-center">
        <a-input-group compact>
          <a-input
            v-model:value="inputMessage"
            placeholder="输入你的问题..."
            @pressEnter="sendMessage"
            class="message-input"
          />
          <div class="button-group">
            <a-button type="primary" @click="sendMessage" class="send-btn">发送</a-button>
            <a-button danger @click="handleEnd" class="end-btn">结束</a-button>
          </div>
        </a-input-group>
      </div>
    </a-layout-footer>
  </a-layout>
</template>

<script setup>
import { ref, computed, onMounted, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import axios from 'axios'

const route = useRoute()
const roomId = ref(route.params.roomId)
const messages = ref([])
const inputMessage = ref('')
const messageListRef = ref(null)
const isGameStarted = ref(false)
const isGameEnded = ref(false)

// 自动滚动到底部
const scrollToBottom = () => {
  nextTick(() => {
    if (messageListRef.value) {
      messageListRef.value.scrollTop = messageListRef.value.scrollHeight
    }
  })
}

// 发送消息
const sendMessage = async () => {
  if (!inputMessage.value.trim()) return

  try {
    // 用户消息
    messages.value.push({
      content: inputMessage.value,
      isAI: false
    })

    // AI 回复
    const response = await axios.post(
      `http://175.178.59.14:8081/api/chat/${roomId.value}/send`,  //线上服务器接口
      null,
      { params: { message: inputMessage.value } }
    )

    messages.value.push({
      content: response.data,
      isAI: true
    })

    // 检查游戏结束
    if (response.data.includes('游戏已结束')) {
      isGameEnded.value = true
    }

    inputMessage.value = ''
    scrollToBottom()
  } catch (error) {
    console.error('发送消息失败:', error)
  }
}

// 处理开始游戏
const handleStart = async () => {
  isGameStarted.value = true
  await sendMessage()
}

// 处理结束游戏
const handleEnd = () => {
  isGameEnded.value = true
  messages.value.push({
    content: '游戏已结束',
    isAI: true
  })
}
</script>

<style scoped>
/* 全屏容器 */
.chat-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: url('/images/bg-chat.jpg') no-repeat center center/cover;
  display: flex;
  flex-direction: column;
}

/* 居中内容容器 */
.chat-center {
  max-width: 800px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
}

/* 房间信息 */
.room-info {
  text-align: center;
  margin-bottom: 20px;
  background: rgba(255, 255, 255, 0.9);
  padding: 12px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.room-id {
  font-size: 1.2rem;
  color: #1890ff;
  font-weight: 500;
}

.room-tip {
  color: #666;
  margin-top: 4px;
}

/* 消息列表 */
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 消息项 */
.message-item {
  margin-bottom: 16px;
  display: flex;
}

.message-item.user {
  justify-content: flex-end;
}

/* 消息气泡 */
.message-bubble {
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 18px;
  line-height: 1.5;
  position: relative;
  word-break: break-word;
}

/* AI消息样式 */
.ai .message-bubble {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 18px 18px 18px 4px;
}

/* 用户消息样式 */
.user .message-bubble {
  background: #1890ff;
  color: white;
  border-radius: 18px 18px 4px 18px;
}

/* 头像样式 */
.message-avatar {
  margin-top: 4px;
}

/* 输入区域 */
.input-area {
  background: rgba(255, 255, 255, 0.9);
  padding: 16px 0;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.08);
}

.input-center {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 20px;
}

.message-input {
  border-radius: 20px;
  padding: 8px 16px;
}

.button-group {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.send-btn {
  border-radius: 20px;
  padding: 0 20px;
}

.end-btn {
  border-radius: 20px;
  padding: 0 20px;
}

/* 滚动条美化 */
.message-list::-webkit-scrollbar {
  width: 6px;
}

.message-list::-webkit-scrollbar-track {
  background: rgba(0,0,0,0.05);
  border-radius: 3px;
}

.message-list::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.2);
  border-radius: 3px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-center {
    padding: 12px;
  }

  .message-bubble {
    max-width: 85%;
  }

  .input-center {
    padding: 0 12px;
  }

  .button-group {
    flex-direction: column;
  }

  .message-input {
    border-radius: 12px;
  }

  .send-btn,
  .end-btn {
    width: 100%;
  }
}
</style>
